/*******************************************************************************
 * Copyright (c) 1998, 2017 IBM Corp. and others
 *
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which accompanies this
 * distribution and is available at https://www.eclipse.org/legal/epl-2.0/
 * or the Apache License, Version 2.0 which accompanies this distribution and
 * is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * This Source Code may also be made available under the following
 * Secondary Licenses when the conditions for such availability set
 * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
 * General Public License, version 2 with the GNU Classpath
 * Exception [1] and GNU General Public License, version 2 with the
 * OpenJDK Assembly Exception [2].
 *
 * [1] https://www.gnu.org/software/classpath/license.html
 * [2] http://openjdk.java.net/legal/assembly-exception.html
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 *******************************************************************************/

#include "jni.h"
#include "jcl.h"
#include "jclglob.h"
#include "jclprots.h"
#include "jcl_internal.h"

extern "C" {

/* java.lang.ref.Reference: private native void reprocess(); */
void JNICALL
Java_java_lang_ref_Reference_reprocess(JNIEnv *env, jobject recv)
{
	J9VMThread* currentThread = (J9VMThread*)env;
	J9JavaVM* vm = currentThread->javaVM;
	J9InternalVMFunctions* vmFuncs = vm->internalVMFunctions;
	J9MemoryManagerFunctions* mmFuncs = vm->memoryManagerFunctions;
	j9object_t receiverObject = (j9object_t)recv;
	jboolean fastJNI = JNI_NATIVE_CALLED_FAST(env);
	if (!fastJNI) {
		vmFuncs->internalEnterVMFromJNI(currentThread);
		receiverObject = J9_JNI_UNWRAP_REFERENCE(recv);
	}
	if (J9_GC_POLICY_METRONOME == vm->gcPolicy) {
		/* Under metronome call getReferent, which will mark the referent if a GC is in progress. */
		mmFuncs->j9gc_objaccess_referenceGet(currentThread, receiverObject);
	} else {
		/* Reprocess this object if a concurrent GC is in progress */
		mmFuncs->J9WriteBarrierBatchStore(currentThread, receiverObject);
	}
	if (!fastJNI) {
		vmFuncs->internalReleaseVMAccess(currentThread);
	}
}

}
